<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        :placeholder="lang.systemManage.role.index.filters.roleNamePlaceholder"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ lang.systemManage.role.index.filters.searchBtn }}
      </el-button>
      <el-button v-waves class="filter-item" type="default" icon="el-icon-refresh" @click="tableReset()">
        {{ lang.systemManage.role.index.filters.refreshBtn }}
      </el-button>
    </div>
    <div class="filter-container">
      <el-button v-permission="'sys-role-add'" v-waves class="filter-item right-btns" type="success" @click="add()">
        <svg-icon icon-class="add"/> {{ lang.systemManage.role.index.operationBtn.create }}
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="listQuery.list"
      :stripe="true"
      :border="true"
      fit
      highlight-current-row
      style="width: 100%;">
      <el-table-column type="index" align="center"/>
      <el-table-column :label="lang.systemManage.role.index.tableCol.roleName" align="left" min-width="100px">
        <template slot-scope="scope">
          <span class="link-type" @click="showDetail(scope.row)">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="lang.systemManage.role.index.tableCol.remark" align="left" class-name="small-padding fixed-width" min-width="200px">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="lang.systemManage.role.index.tableCol.operation.name" align="center" class-name="small-padding fixed-width" width="260px">
        <template slot-scope="scope">
          <el-button v-permission="'sys-role-edit'" v-waves type="primary" plain size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button v-permission="'sys-role-grant'" v-waves type="primary" plain size="mini" @click="grant(scope.row)">设置权限</el-button>
          <el-button v-permission="'sys-role-delete'" v-waves :disabled="scope.row.isBuiltin === '1'" plain type="danger" size="mini" @click="del(scope.row)">
            {{ lang.systemManage.role.index.tableCol.operation.delete }}
          </el-button>
          <!-- <el-button v-waves v-if="$$roleBtn('systemManage.role.del')" type="danger" plain size="mini" @click="del(scope.row)">删除</el-button> -->
          <!-- <el-button v-waves v-if="scope.row.enable === 1 " type="danger" plain size="mini" @click="statusChose(scope.row)">禁用</el-button> -->
          <!-- <el-button v-waves v-else type="primary" plain size="mini" @click="statusChose(scope.row)">启用</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="listQuery.total>0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.rows"
      @pagination="getList" />

    <detail-dialog ref="detailDialog"/>
    <add-edit-dialog ref="editDialog" :loading="isEditSubmitting" @ok="handleEditOk"/>
    <grant-dialog ref="grantDialog" :loading="isGrantSubmitting" @ok="handleGrantOk"/>

  </div>
</template>

<script>
import { roleAdd, roleUpdate, roleGrant, getRolePages, deleteRole, roleUpdateDisabled } from '@/api/systemManage/role'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import DelBoxMixin from '@/mixins/delBox'
import { mapGetters } from 'vuex'

import detailDialog from './detail'
import addEditDialog from './add-edit'
import grantDialog from './grant'

export default {
  name: 'RoleManage',
  components: { Pagination, detailDialog, addEditDialog, grantDialog },
  directives: { waves },
  mixins: [DelBoxMixin],
  data() {
    return {
      listLoading: true,
      listQuery: {},

      isEditSubmitting: false,
      isGrantSubmitting: false
    }
  },
  computed: {
    ...mapGetters([
      'lang'
    ])
  },
  created() {
    this.tableReset()
  },
  methods: {
    tableReset() {
      this.listQuery = {
        page: 1,
        rows: 10,
        total: 0,
        roleName: null,
        list: null
      }
      this.getList()
    },
    getList() {
      this.listLoading = true
      const params = Object.assign({}, this.listQuery)
      delete params.total
      delete params.list
      getRolePages(params).then(response => {
        this.listQuery.list = response.data.list
        this.listQuery.total = response.data.total
        this.listLoading = false
      }).catch(e => {
        this.listLoading = false
        this.$message.error(e.message)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    showDetail(item) {
      if (item && item.id) {
        this.$refs.detailDialog.show({
          id: item.id
        })
      }
    },

    add() {
      this.isEditSubmitting = false
      this.$refs.editDialog.show()
    },

    edit(item) {
      // const type = item ? 'detail' : 'create'
      // const id = item ? item.id : 0
      // this.$router.push(`/system-manage/role-manage/${type}/${id}`)
      this.isEditSubmitting = false
      if (item && item.id) {
        this.$refs.editDialog.show({
          id: item.id
        })
      }
    },

    handleEditOk(formData, isEdit) {
      const invoke = isEdit ? roleUpdate : roleAdd

      this.isEditSubmitting = true
      invoke(formData).then(resp => {
        this.isEditSubmitting = false
        this.$refs.editDialog.close()
        this.$message.success(resp.message)
        this.getList()
      }).catch(resp => {
        this.isEditSubmitting = false
        this.$message.error(resp.message)
      })
    },

    grant(item) {
      this.isGrantSubmitting = false
      if (item && item.id) {
        this.$refs.grantDialog.show({
          roleid: item.id
        })
      }
    },

    handleGrantOk(formData) {
      roleGrant(formData).then(resp => {
        this.isGrantSubmitting = false
        this.$refs.grantDialog.close()
        this.$message.success(resp.message)
        // this.getList()
      }).catch(resp => {
        this.isGrantSubmitting = false
        this.$message.error(resp.message)
      })
    },

    del(item) {
      if (item.type === -1) return this.$message.warning('您不可以删除此类型用户！')
      this.$$delBox().then(() => {
        deleteRole(item.id).then(res => {
          if (res.code === 200) {
            this.$message.success(res.message)
            this.getList()
          } else this.$message.error(res.message)
        }).catch(e => {
          this.$message.success(this.$store.getters.errorMsg)
        })
      })
    },
    statusChose(item) {
      item.$statusDisabled = true
      roleUpdateDisabled(item.id, item.enable).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.getList()
        } else this.$message.error(res.message)
      }).catch(e => {
        this.$message.success(this.$store.getters.errorMsg)
      })
    }
  }
}
</script>
